/**
 *
 * 人族防御塔
 * @author 
 *
 */
class HumanTower extends TowersBase{
    
    private st1: ArrowShooter01;
    private st2: ArrowShooter01;
    
    /**武器*/
    private weapon: Arrow01;
    
    /**纹理对象*/
    public bm: egret.Bitmap;
    public lastHP:number = 0;
    
    //每次点击生成长度为1的路径寻路点
	public constructor() {
        super();
        
	}
    public addTexture(tietu: string,name:string) {
        this.nameInfo = name;
        this.minRadius = 100;
        this.maxRadius = 140;
        this.ratioY = this.minRadius / this.maxRadius;
        this.bm = Utils.createBitmapByName(tietu);
        this.addChild(this.bm);
        this.anchorOffsetX = this.width / 2;
        this.anchorOffsetY = this.height;
        
        //血条位置
        this.lifeBar.x = 32;
        this.lifeBar.y = 5;
        
        ////创建左右ArrowShooter01
        this.st1 = new ArrowShooter01();
        this.st1.x = 36;
        this.st1.y = 5;
        this.addChild(this.st1);
        this.st1.anchorOffsetX = this.st1.width / 2;

        this.st2 = new ArrowShooter01();
        this.st2.x = 50;
        this.st2.y = 5;
        this.addChild(this.st2);
        this.offy = 10;
        this.st2.anchorOffsetX = this.st2.width / 2;
        this.fireDelay = 1000;
        
        this.lastHP = this.life;

    }
	
    /**创建-初始化*/
    public onCreate(){
        this.hp = this.life;
        this.posArr = [];
        this._pathIndex = 0;
        
        this.timesum = 0;
        this.lifeBar.reSet();
        this.canClear = false;
        
        this.atjijie = false;
        this.moveToTarget = false;
        this.unitType = 4;

        
       // this.fsm.changeState(stateType.idleState);
       // this.curState = stateType.idleState;
        
        //this.view.touchEnabled = true;
        //this.view.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.viewSelect,this);
    }
    
    
    /**实时刷新*/
    public onEnterFrame(advancedTime: number) {
        super.onEnterFrame(advancedTime);
        //实时刷新射手动作
        this.st1.onEnterFrame(advancedTime);
        this.st2.onEnterFrame(advancedTime);
        
        if(this.nameInfo == "myLord" && (this.lastHP - this.hp)/this.life>0.2) {
            this.lastHP=this.hp
            GuankaBase.instance.heroSound("WellPlayed","恩~打得不错！",1100);
        }
        
        this.time++;
        if(this.time > 240 && this.hp < this.life&&this.hp>0) {
            this.hp += this.life * 0.01;

            this.time =0;
        }
        //进入范围敌人数组为空则直接返回
        
        if(this.atargets.length == 0||this.hp<0) {
            this.timesum = this.fireDelay;
            return;
        }
        // console.log(this.atargets.length+":");
        
        //发射间隔
        this.timesum += advancedTime;
        if(this.timesum < this.fireDelay) {
            return;
        }
        this.timesum = 0;
        
        //取进入范围数组第一个
        this.target = this.atargets[0];
        //确定敌人方向
        if(this.target.x >= this.sx && this.target.y <= this.sy - 22) {
            this.direct = "upR";
        }
        if(this.target.x >= this.sx && this.target.y > this.sy - 22) {
            this.direct = "downR";
        }
        if(this.target.x < this.sx && this.target.y <= this.sy - 22) {
            this.direct = "upL";
        }
        if(this.target.x < this.sx && this.target.y > this.sy - 22) {
            this.direct = "downL";
        }
        //射击音效播放
        this.playFireVoice();
        //轮流射击
        var p1: egret.Point;
        if(this.firstst == "L") {
            this.st1.fire(this.direct);
            this.firstst = "R";
            //弓箭产生坐标点
            p1 = new egret.Point(this.sx - 5,this.sy - 46);
        } else if(this.firstst == "R") {
            this.st2.fire(this.direct);
            this.firstst = "L";
            //弓箭产生坐标点
            p1 = new egret.Point(this.sx + 5,this.sy - 46);
        }
        //利用对象池产生弓箭对象并进行碰撞检测
        this.weapon = <Arrow01>ObjectPool.getInstance().createObject(Arrow01);
        //this.weapon.damage = 4 * GuankaBase.instance.hardxs2;
        this.weapon.damage = this.damage;
        this.weapon.init(p1,this.target,this.target.offy);
        GuankaBase.instance.weaponLayer.addChild(this.weapon);
        //this.target.value = Math.floor(this.target.value / 2);
        
        if(this.weapon.damage >= this.target.hp) {
            var idx: number = this.targets.indexOf(this.target);
            if(idx != -1)
                this.targets.splice(idx,1);
            idx = this.atargets.indexOf(this.target);
            if(idx != -1)
                this.atargets.splice(idx,1);
            this.target = null;
        }
    }
    
    /**销毁*/
    public onDestroy(): void {
        if(this.nameInfo == "myTower") {
            GuankaBase.instance.heroSound("sorry","呃……抱歉……",800);
        }
        super.onDestroy();
        //this.view.touchEnabled = false;
        //this.view.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.viewSelect,this);
    }
    
}
